﻿using System;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace RXY_Mall.Core.Tool
{
    public class EmailHelper
    {
        private static string emailAcount= "haohengzhen@163.com";
        private static string emailPassword= "haobingfeixiang";

        public static void SendEmail(string subject, Exception currentEx)
        {
            try
            {
                MailMessage message = new MailMessage();
                                
                //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
                MailAddress fromAddr = new MailAddress(emailAcount);
                
                message.From = fromAddr;
                //设置收件人,可添加多个,添加方法与下面的一样
                message.To.Add(emailAcount);
                //设置抄送人
                //message.CC.Add("");
                //设置邮件标题
                message.Subject = subject;
                //设置邮件内容
                StringBuilder sbStr = new StringBuilder();
                sbStr.Append("六合一道");
                sbStr.AppendLine("-------------------------------------------------");
                sbStr.AppendLine("错误发生时间：" + DateTime.Now.ToString());
                sbStr.AppendLine("错误信息：" + currentEx.Message.ToString());
                sbStr.AppendLine("错误源：" + currentEx.StackTrace.ToString());
                message.Body = sbStr.ToString();

                //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看
                using (SmtpClient client = new SmtpClient("smtp.163.com", 25))
                {
                    //设置发送人的邮箱账号和密码
                    client.Credentials = new NetworkCredential(emailAcount, emailPassword);
                    //启用ssl,也就是安全发送
                    //client.EnableSsl = true;
                    //发送邮件
                    client.Send(message);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString());
            }

        }
    }
}
